/**
 * 登录
 * @author tiedong
 * @since 2024/02/20
 */
layui.use(['index', 'table','form'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var form = layui.form

    // 渲染数据
    table.render({
        elem: '#table-index'
        , url: '/admin/role/getlist'
        , toolbar: '#toolbar'
        , defaultToolbar: ['filter', 'exports', 'print']
        , height: 'full-100' // 最大高度减去其他容器已占有的高度差
        , cellMinWidth: 80
        , page: false
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: 'ID', sort: true}
            , {field: 'title', width: 200, title: '角色名称'}
            , {field: 'status', width: 150, title: '状态', templet: '#status-switchTpl'}
            , {field: 'addTime', title: '添加时间', width: 200}
            , { title: '操作',minWidth: 200, toolbar: '#bar-handle'}
        ]]
        , error: function (res, msg) {
            console.log(res, msg)
        }
    });

    // 工具栏事件
    table.on('toolbar(table-index)', function (obj) {
        var id = obj.config.id;
        var checkStatus = table.checkStatus(id);
        var othis = lay(this);
        switch (obj.event) {
            case 'add':
                active[obj.event] ? active[obj.event]("添加角色"): '';
                break;
            case 'b-del':
                var data = checkStatus.data;
                if (data.length == 0) {
                    layer.msg('请选择要删除的数据', {icon: 1})
                    return false;
                }
                var ids = '';
                data.forEach(function (item,index){
                    ids += item.id+","
                })
                ids = ids.slice(0,-1)
                active["del"] ? active["del"](ids): '';
                break;
        }
    });

    //触发单元格工具事件
    table.on('tool(table-index)', function (obj) {
        var data = obj.data;
        console.log(obj)
        switch (obj.event) {
            case 'edit':
                active[obj.event] ? active[obj.event]("编辑角色",data): '';
                break;
            case 'del':
                layer.confirm('是否真的删除数据', function () {
                    layer.msg('正在删除', {
                        icon: 16,
                        timeout:1000
                    }, function(){
                        active[obj.event] ? active[obj.event](data.id): '';
                    });
                });
                break;
            case 'auth':
                active[obj.event] ? active[obj.event](data): '';
                break;
        }
    });

    /* 事件-指定开关 */
    form.on('switch(status-switch)', function(obj){
        active["status"] ? active["status"](obj): '';
    });

    //事件处理
    var active = {
        //添加
        add:function (title){
            layer.open({
                type: 2
                ,title: title
                ,content: '/admin/role/add'
                ,maxmin: true
                ,area: ['550px', '450px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    submit.click();
                }
            });
        },
        //编辑
        edit:function (title,data){
            json = JSON.stringify(data);
            layer.open({
                type: 2
                ,title: title
                ,content: '/admin/role/edit?id='+data.id
                ,maxmin: true
                ,area: ['550px', '450px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    submit.click();
                }
            });
        },
        //修改状态
        status:function (obj){
            var id = obj.value;
            if (id==9){
                layer.msg("超级管理员不能关闭", {
                    icon: 5,
                    time: 1500,
                });
                return false;
            }
            var status = obj.elem.checked ? 1 : 0;
            $.ajax({
                url:"/admin/role/status",
                method: "POST",
                data: {id:id,status:status},
                success: function(res) {
                    if(res.code == 0){
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 1500,
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 1500
                        });
                    }
                },
                error: function(e) {
                    layer.msg("修改失败");
                }
            });
        },
        //删除
        del:function (ids) {
            $.ajax({
                url:"/admin/role/del",
                method: "GET",
                data: {ids:ids},
                success: function(res) {
                    if(res.code == 0){
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 1500,
                        },function () {
                            table.reload('table-index'); //重载表格
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 1500
                        });
                    }
                },
                error: function(e) {
                    layer.msg("删除失败");
                }
            });
        },
        //权限
        auth:function (data){
            json = JSON.stringify(data);
            layer.open({
                type: 2
                ,title: '权限设置（角色名：'+data.title+')'
                ,content: '/admin/role/auth?id='+data.id
                ,maxmin: true
                ,area: ['450px', '550px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    submit.click();
                }
            });
        },
    }

});